package com.sunny.utils;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import ws.schild.jave.EncoderException;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.info.MultimediaInfo;

import java.io.File;
import java.io.IOException;

public class VideoUtils {

    public static String secondToTime(int time) {
        String timeStr = null;
        int hour = 0;
        int minute = 0;
        int second = 0;
        if (time <= 0)
            return "00:00:00";
        else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = "00:" + unitFormat(minute) + ":" + unitFormat(second);
            } else {
                hour = minute / 60;
                /*if (hour > 99)
                    return "59:59:59";*/
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            }
        }
        return timeStr;
    }

    private static String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + i;
        else
            retStr = "" + i;
        return retStr;
    }

    public static int timeToSecond(String timeStr) {
        if (timeStr == null) {
            return 0;
        }
        String[] timeArray = timeStr.split(":");
        if (timeArray.length != 3) {
            return 0;
        }
        int hour = 0, minute = 0, second = 0;
        try {
            hour = Integer.parseInt(timeArray[0]);
            minute = Integer.parseInt(timeArray[1]);
            second = Integer.parseInt(timeArray[2]);
        } catch (Exception e) {
            return 0;
        }
        return hour * 3600 + minute * 60 + second;
    }

    public static void printMetadata(File f) {
        Metadata metadata = null;
        try {
            metadata = ImageMetadataReader.readMetadata(f);
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (Directory directory : metadata.getDirectories()) {
            for (Tag tag : directory.getTags()) {
                System.out.format("[%s] - %s = %s",
                        directory.getName(), tag.getTagName(), tag.getDescription());
                System.out.println();

                if (tag.getTagName().equals("Detected File Type Name")) {
                    System.out.println("文件类型：" + tag.getDescription());
                }
            }
            if (directory.hasErrors()) {
                for (String error : directory.getErrors()) {
                    System.err.format("ERROR: %s", error);
                }
            }
        }
    }

    public static int getVideoSeconds(File f) {
        Metadata metadata = null;
        try {
            metadata = ImageMetadataReader.readMetadata(f);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

        String fileType = "";
        for (Directory directory : metadata.getDirectories()) {
            if ("File Type".equals(directory.getName())) {
                for (Tag tag : directory.getTags()) {
                    if ("Detected File Type Name".equals(tag.getTagName())) {
                        System.out.println("文件类型：" + tag.getDescription());
                        fileType = tag.getDescription();
                    }
                }
            }
        }
        String duration = "";
        if ("AVI".equals(fileType)) {
            for (Directory directory : metadata.getDirectories()) {
                if ("AVI".equals(directory.getName())) {
                    for (Tag tag : directory.getTags()) {
                        if ("Duration".equals(tag.getTagName())) {
                            System.out.println("视频长度：" + tag.getDescription());
                            duration = tag.getDescription();
                        }
                    }
                }
            }
        } else if ("MP4".equals(fileType)) {
            for (Directory directory : metadata.getDirectories()) {
                if ("MP4".equals(directory.getName())) {
                    for (Tag tag : directory.getTags()) {
                        if ("Duration in Seconds".equals(tag.getTagName())) {
                            System.out.println("视频长度：" + tag.getDescription());
                            duration = tag.getDescription();
                        }
                    }
                }
            }
        } else if ("FLV".equals(fileType) || "ASF".equals(fileType)) {
            //WMV文件识别文件类型为ASF
            return getVideoDuration(f);

        }
        int second = timeToSecond(duration);
        return second;
    }

    public static int getVideoDuration(File source) {
        MultimediaObject multimediaObject = new MultimediaObject(source);
        MultimediaInfo info = null;
        try {
            info = multimediaObject.getInfo();
        } catch (EncoderException e) {
            e.printStackTrace();
            return 0;
        }
        //转换成秒返回
        return (int) (info.getDuration() / 1000);
    }

}